package com.shanwen.shopping.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.classic.core.activity.BaseActivity;
import com.classic.core.log.Logger;
import com.classic.core.utils.DataUtil;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.base.OkHttpUtils;
import com.shanwen.shopping.R;
import com.shanwen.shopping.dialog.QrCodeDialog;
import com.shanwen.shopping.entity.BannerInfo;
import com.shanwen.shopping.entity.GoodsClassify;
import com.shanwen.shopping.entity.ModuleInfo;
import com.shanwen.shopping.http.ActionHelp;
import com.shanwen.shopping.http.ListCallback;
import com.shanwen.shopping.utils.EventUtil;
import com.shanwen.shopping.utils.Utils;
import com.shanwen.shopping.view.BannerView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 首页
 */
public class NewMainActivity extends BaseActivity {

    @BindView(R.id.banner)
    BannerView banner;
    @BindView(R.id.iVCharge)
    ImageView iVCharge;
    @BindView(R.id.iVDaiGou)
    ImageView iVDaiGou;
    @BindView(R.id.iVXiaZai)
    ImageView iVXiaZai;
    @BindView(R.id.iVGongGao)
    ImageView iVGongGao;
    @BindView(R.id.iVZhengWu)
    ImageView iVZhengWu;
    @BindView(R.id.iVZhiNan)
    ImageView iVZhiNan;
    private Unbinder unbinder;
    private List<BannerInfo> mBannerList;
    private List<View> viewList;
    private List<ModuleInfo> moduleInfoList = null;
    private QrCodeDialog dialog = null;

    @Override
    public int getLayoutResId() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_main_new;
    }

    @OnClick({R.id.iVCharge, R.id.iVDaiGou, R.id.iVXiaZai, R.id.iVGongGao, R.id.iVZhengWu, R.id.iVZhiNan})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.iVDaiGou://商品代购
                startActivity(activity, GoodsActivity.class);
                break;
            case R.id.iVCharge://充值中心
                startActivity(activity, RechargeActivity.class);
                break;
            case R.id.iVXiaZai:
                dialog = new QrCodeDialog(NewMainActivity.this);
                dialog.show();
                break;
            case R.id.iVGongGao:
                toEnterpriseList(0);
                break;
            case R.id.iVZhengWu:
                toEnterpriseList(1);
                break;
            case R.id.iVZhiNan:
                toEnterpriseList(2);
                break;
        }
    }

    @Override
    public void initView() {
        super.initView();
        unbinder = ButterKnife.bind(this);
        mBannerList = new ArrayList<>();
        viewList = new ArrayList<>();
        moduleInfoList = new ArrayList<>();
        getModules();
        getBanner();

    }

    /**
     * 跳转到企业动态列表界面
     */
    private void toEnterpriseList(int index) {
        if (moduleInfoList.size() == 0) {
            ToastUtil.showLongToast(this, "请重新刷新界面");
            return;
        }
        Intent intent = new Intent(this, EnterpriseListActivity.class);
        intent.putExtra(EnterpriseListActivity.MODULE_INFO, moduleInfoList.get(index));
        startActivity(intent);
    }

    @Override
    public void initData() {
        super.initData();
    }


    @Override
    public void register() {
        EventUtil.registerEventBus(this);
    }

    @Override
    public void unRegister() {
        unbinder.unbind();
        EventUtil.unRegisterEventBus(this);
    }

    @Override
    protected void onDestroy() {
        OkHttpUtils.cancelTag(this);
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    private void getBanner() {
        ActionHelp.qryEnterpriseZoneBanner(activity, "3", new ListCallback<BannerInfo>() {
            @Override
            public void onSuccess(List<BannerInfo> response) {
                mBannerList.clear();
                if (!DataUtil.isEmpty(response)) {
                    mBannerList.addAll(response);
                }
                for (int i = 0; i < mBannerList.size(); i++) {
                    ImageView image = new ImageView(NewMainActivity.this);
                    image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    image.setScaleType(ImageView.ScaleType.FIT_XY);
                    Picasso.with(NewMainActivity.this).load(mBannerList.get(i).getImgUrl()).into(image);
                    viewList.add(image);
                }
                banner.startLoop(true);
                banner.setViewList(NewMainActivity.this, viewList, mBannerList);
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Logger.e("获取banner失败,code:" + code + ",message:" + errorMessage);
            }

            @Override
            public Class<BannerInfo> getType() {
                return BannerInfo.class;
            }
        });
    }


    private void getModules() {
        ActionHelp.qryModuleList(activity, "3", 3, new ListCallback<ModuleInfo>() {
            @Override
            public void onSuccess(List<ModuleInfo> response) {
                if (!DataUtil.isEmpty(response)) {
                    moduleInfoList.clear();
                    moduleInfoList.addAll(response);
                    moduleInfoList.get(0).setModuleName("公告通知");
                    moduleInfoList.get(1).setModuleName("政务公开");
                    moduleInfoList.get(2).setModuleName("服务指南");
                }
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Logger.e(code + "," + errorMessage);
                Utils.showToast(activity, "获取新闻分类失败");
            }

            @Override
            public Class<ModuleInfo> getType() {
                return ModuleInfo.class;
            }
        });
    }
}
